<script setup lang="ts">
import { ref } from "vue";
import { useMapStore } from "@/store/modules/map";
import { MAP_ACTION } from "@/views/pipe-network/constants";

const mapStore = useMapStore();
const { mapAction } = storeToRefs(mapStore);
const addPoint = (item: any) => {
  mapStore.setShowPipeInfoListFlag(false); // 添加管段时关闭管线、管点弹框
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const addPipeline = (item: any) => {
  mapStore.setShowPipeInfoListFlag(false); // 添加管段时关闭管线、管点弹框
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const connectionTwoPoint = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const addPointOnline = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const devideLineWithPoint = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const editFeature = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const moveFeature = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const deleteFeature = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const searchSinglePoint = (item: any) => {
  mapStore.setMapAction(item.active ? "" : item.iconClass);
  setActiveState(item);
};
const setActiveState = (item: any) => {
  if (!item.active) {
    items.value.forEach((item: any) => (item.active = false));
  }
  item.active = !item.active;
};
const items = ref([
  {
    name: "添加管点",
    iconClass: MAP_ACTION.ADD_POINT,
    active: false,
    click: addPoint,
  },
  {
    name: "添加管段",
    iconClass: MAP_ACTION.ADD_PIPELINE,
    active: false,
    click: addPipeline,
  },
  {
    name: "连接两点",
    iconClass: MAP_ACTION.CONNECTION_POINT_TWO,
    active: false,
    click: connectionTwoPoint,
  },
  {
    name: "线上加点",
    iconClass: MAP_ACTION.ADD_POINT_ONLINE,
    active: false,
    click: addPointOnline,
  },
  {
    name: "点打断线",
    iconClass: MAP_ACTION.DEVIDE_LINE_WITH_POINT,
    active: false,
    click: devideLineWithPoint,
  },
  {
    name: "修改信息",
    iconClass: MAP_ACTION.EDIT,
    active: false,
    click: editFeature,
  },
  {
    name: "移动",
    iconClass: MAP_ACTION.MOVE,
    active: false,
    click: moveFeature,
  },
  {
    name: "删除",
    iconClass: MAP_ACTION.DELETE,
    active: false,
    click: deleteFeature,
  },
  {
    name: "孤点检测",
    iconClass: MAP_ACTION.SEARCH_SINGLE_POINT,
    click: searchSinglePoint,
  },
]);
onUnmounted(() => mapStore.setMapAction(""));
watchEffect(() => {
  if (mapAction.value === "") {
    items.value.forEach((item) => {
      if (item.active) {
        item.active = false;
      }
    });
  }
});

onMounted(() => {
  console.log(items.value);
});
</script>

<template>
  <div class="edit-tools">
    <div
      v-for="(item, index) in items"
      class="tool"
      @click="item.click(item)"
      :key="index"
      :style="
        item.active ? { color: 'blue', width: '60px' } : { width: '60px' }
      "
    >
      <svg-icon alt="" size="24" :icon-class="item.iconClass" color="black" />
      <span :style="item.active ? { color: 'blue' } : { width: 'white' }">{{
        item.name
      }}</span>
    </div>
  </div>
</template>

<style scoped lang="scss">
.edit-tools {
  position: absolute;
  top: 17px;
  left: 513px;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: calc(100% - 740px);
  max-width: 858px;
  height: 80px;
  padding-top: 10px;
  font-family: Roboto, sans-serif;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  background-color: rgb(120 121 125 / 80%);
  border: 1px solid rgb(187 187 187 / 100%);
}

.tool {
  display: flex;
  flex-direction: column;
  place-items: center center;
  width: 56px;
  font-size: 14px;
  color: #fff;
  cursor: pointer;

  span {
    height: 20px;
    margin-top: 8px;
    line-height: 20px;
    color: #fff;
  }
}
</style>
